package com.gton.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gton.mapper.GpLoginMapper;
import com.gton.entity.GpLogin;
import com.gton.service.GpLoginService;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gton.io.BaseLimitRequest;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.List;

/**
 * (GpLogin)表服务实现类
 *
 * @author 郭童
 * @since 2023-07-07 15:22:25
 */
@Service("gpLoginService")
public class GpLoginServiceImpl extends ServiceImpl<GpLoginMapper, GpLogin> implements GpLoginService {

    @Resource
    private GpLoginMapper gpLoginMapper;
    
    
    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: GuoTong
     * @date: 2022-12-02 14:57:06
     * @return:com.baomidou.mybatisplus.core.metadata.IPage
     */
    @Override
    public IPage<GpLogin> queryLimitPage(BaseLimitRequest<GpLogin> limitRequest) {
        GpLogin requestBody = limitRequest.getRequestBody();
        long pageIndex = limitRequest.getPageIndex();
        long pageSize = limitRequest.getPageSize();
        IPage<GpLogin> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<GpLogin> queryWrapper = new QueryWrapper<>();
        byte[] bytes = JSON.toJSONBytes(requestBody);
        JSONObject thisObj = JSON.parseObject(bytes);
        Set<Map.Entry<String, Object>> entries = thisObj.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            StringBuilder column = new StringBuilder();
            char[] chars = key.toCharArray();
            for (char aChar : chars) {
                if (Character.isUpperCase(aChar)) {
                    column.append("_");
                }
                column.append(aChar);
            }
            queryWrapper.eq(getValueIsBoolean(entry.getValue()), column.toString(), entry.getValue());
        }
        LocalDateTime beginTime = limitRequest.getBeginTime();
        LocalDateTime endTime = limitRequest.getEndTime();
        queryWrapper.ge(getValueIsBoolean(beginTime), "create_time", beginTime);
        queryWrapper.le(getValueIsBoolean(endTime), "create_time", beginTime);
        return gpLoginMapper.selectPage(page, queryWrapper);
    }
    
      /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: GuoTong
     * @date: 2022-12-06 19:54:59
     * @return:java.lang.Integer
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<GpLogin> entityList) {

        return gpLoginMapper.insertBatchSomeColumn(entityList);
    }
    
    private boolean getValueIsBoolean(Object object) {

        if (object instanceof String) {
            return StringUtils.isNotEmpty((CharSequence) object);
        }
        return !Objects.isNull(object);
    }
}

